package com.jiezhuo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiezhuo.easyExcel.entity.TeacherExcel;
import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.cmeduSchool.Jzg;
import com.jiezhuo.entity.cmeduSchool.TJzg;
import com.jiezhuo.entity.jzg.*;

import com.jiezhuo.mapper.center.TJzgMapper;
import com.jiezhuo.service.jzg.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiezhuo.utils.R;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author com.jiezhuo
 * @since 2024-06-05
 */
@Service
public class TJzgServiceImpl extends ServiceImpl<TJzgMapper, TJzg> implements TJzgService {
    @Autowired
    TJzgUserService tJzgUserService;

    @Override
    public List<HashMap<String, Object>> getOnGuardFaculty(JzgVO jzgVO) {
        return this.baseMapper.getOnGuardFaculty(jzgVO);
    }

    @Override
    public int getOnGuardFacultyCount(JzgVO jzgVO) {
        return this.baseMapper.getOnGuardFacultyCount(jzgVO);
    }

    @Override
    public R getOnGuardFacultyInfo(JzgVO jzgVO) {
        HashMap<String, Object>onGuardFacultyInfo = this.baseMapper.getOnGuardFacultyInfo(jzgVO);
        return R.ok().data("jzgInfo",onGuardFacultyInfo);
    }

    @Override
    public boolean getCard(String sfzjh) {
        return this.baseMapper.getCard(sfzjh);
    }

    @Override
    public HashMap<String, Object> getCardInfo(String sfzjh) {
        return this.baseMapper.getCardInfo(sfzjh);
    }

    public static void main(String[] args) {
        int num = 255;
        String hexString = String.format("%08d", num);
        System.out.println(hexString);

    }
    @Override
    @Transactional
    public R saveFaculty(TJzg tJzg) {
        QueryWrapper<TJzg> tJzgQueryWrapper = new QueryWrapper<>();
        tJzgQueryWrapper.eq("SFZJH",tJzg.getSfzjh());
        String id =this.getBaseMapper().selectMaxId();
        int i = Integer.parseInt(id);
        TJzg tJzg1 = this.getBaseMapper().selectOne(tJzgQueryWrapper);
        boolean b =false;
        if(ObjectUtils.isEmpty(tJzg1)){
            tJzg.setTeacherid(String.format("%08d", i+1));
            b= this.save(tJzg);
            TJzgUser tJzgUser = new TJzgUser();
            tJzgUser.setTeacherid(String.format("%08d", i+1));
            tJzgUser.setSyzt("1");
            tJzgUser.setLoginId(tJzg.getXm());
            tJzgUserService.save(tJzgUser);
        }else {
            TJzgUser tJzgUser = new TJzgUser();
            tJzgUser.setSyzt("1");
            tJzgUserService.updateById(tJzgUser);
             b = this.updateById(tJzg);
        }
        return b?R.ok():R.err();
    }

    @Autowired
    JzgGjxxbgService jzgGjxxbgService;
    @Transactional
    @Override
    public boolean updateFacultyId(TJzg tJzg) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        UUID uuid = UUID.randomUUID();
        String string = uuid.toString();
        QueryWrapper<TJzg> tJzgQueryWrapper = new QueryWrapper<>();
        tJzgQueryWrapper.eq("SFZJH",tJzg.getSfzjh());
        TJzg tJzg1 = this.getBaseMapper().selectOne(tJzgQueryWrapper);
        JzgGjxxbg jzgGjxxbg = new JzgGjxxbg();
        jzgGjxxbg.setId(string);
        jzgGjxxbg.setYy(tJzg.getYy());
        jzgGjxxbg.setTeacherid(tJzg1.getTeacherid());
        jzgGjxxbg.setOperator(userId);
        jzgGjxxbg.setOperateTime(new Date());
        jzgGjxxbgService.save(jzgGjxxbg);
        boolean b = this.getBaseMapper().updateFacultyId(tJzg);
        return b;
    }

    @Autowired
    JzgNewMenuService jzgNewMenuService;
    @Autowired
    JzgYdService jzgYdService;
    @Override
    public R    moveFaculty(TJzg jzg) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        QueryWrapper<TJzg> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("SFZJH",jzg.getSfzjh());
        TJzg one = this.getOne(objectQueryWrapper);
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        String string = userPermissionInfo.getData().get("estate").toString();
        JzgYd jzgYd = new JzgYd();
        jzgYd.setYdlczt("0");
        int max=  jzgYdService.getMaxId();
        jzgYd.setId(max+1);
        jzgYd.setTeacherid(one.getTeacherid());
        jzgYd.setYdlx(jzg.getYdlx());
        jzgYd.setSqsj(new Date());
        jzgYd.setFqrId(userId);
        jzgYd.setYxqdm(one.getGzXqdm());
        jzgYd.setHxqdm(jzg.getGzXqdm());

        if (jzg.getYdlx().equals("01")){
            jzg.setDjXqdm(jzg.getGzXqdm());
        }else if(jzg.getYdlx().equals("02")){

        }else if(jzg.getYdlx().equals("10")){

        }else if(jzg.getYdlx().equals("11")){

        }
        if(string.equals("区级")){
            jzgYd.setCzjb(string);
            jzgYd.setYdlczt("1");
            jzgYd.setSjsj(new Date());
            jzgYd.setSjrId(userId);
            jzgYd.setSjz("1");
            boolean b = this.updateById(jzg);
        }
        int insert = jzgYdService.getBaseMapper().insert(jzgYd);

        return R.ok();
    }

    @Override
    public R moveFacultyConsent(JzgYdVO jzg) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        QueryWrapper<JzgYd> jzgYdQueryWrapper = new QueryWrapper<>();
        jzgYdQueryWrapper.eq("ID",jzg.getYdId());
        JzgYd one = jzgYdService.getOne(jzgYdQueryWrapper);
        QueryWrapper<TJzg> jzgQueryWrapper = new QueryWrapper<>();

        if (jzg.getYdConsent()==1){
            String teacherid = one.getTeacherid();
            jzgQueryWrapper.eq("TeacherId",teacherid);
            TJzg tJzg = this.getBaseMapper().selectOne(jzgQueryWrapper);

            one.setZjrId(userId);
            one.setHzsj(new  Date());
            one.setHzz("1");
            one.setSjz("1");
            one.setSjrId(userId);
            if (one.getYdlx().equals("01")){
                tJzg.setDjXqdm(jzg.getGzXqdm());
            }
            tJzg.setGzXqdm(one.getHxqdm());
            this.updateById(tJzg);
        }
        one.setSjrId(userId);
        one.setYdlczt("1");
        int update = jzgYdService.getBaseMapper().update(one, jzgYdQueryWrapper);
        return update>=0? R.ok():R.err();
    }

    @Override
    public R getUserList(UserListVo userListVo) {
        ArrayList<HashMap<String,Object>> userList =this.getBaseMapper().getUserList(userListVo);
        int userCount =this.getBaseMapper().getUuserCount(userListVo);
        return R.ok().data("list",userList).data("total",userCount);

    }

    @Override
    public List<TeacherExcel> getTeacherExcel(JzgVO jzgVO) {
        return this.getBaseMapper().getTeacherExcel( jzgVO);
    }

    @Override
    public List<Map<String, Object>> jssUserSaveLoad(String teacherId) {
        return this.baseMapper.jssUserSaveLoad(teacherId);
    }


}
